package com.webex.ta.hydra.core;

import java.io.File;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-15
 * Time: 12:30:08
 */
public class FileService {
    private static final String DEFAULT_BASE = System.getProperty("user.dir");

    private static FileService instance;

    private File baseDir;

    private FileService() {
        baseDir = new File(DEFAULT_BASE);
    }

    public static FileService getInstance() {
        if (instance == null)
            instance = new FileService();
        return instance;
    }

    public void resetBaseDir() {
        baseDir = new File(DEFAULT_BASE);
    }

    public File getFile(String path) {
        File f = new File(path);
        if (!f.isAbsolute()) {
            f = new File(getBaseDir(), path);
        }
        return f;
    }

    public String getBaseDir() {
        return baseDir.getAbsolutePath();
    }

    public synchronized void setBaseDir(String basedir){
		if (basedir != null) {
			baseDir = new File(basedir);
			if (!baseDir.isDirectory()) {
				baseDir = baseDir.getParentFile();
			}
		} else {
//            resetBaseDir();
        }
	}
}
